package com.booth.manage.controllers;

import java.util.List;

import com.booth.base.controllers.BaseController;
import com.booth.base.model.Groups;
import com.booth.base.model.Users;
import com.booth.base.utils.MD5;
import com.booth.base.utils.ResultStatus;
import com.booth.manage.model.BussOperator;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

/***
 * 	摊位经营者控制器
 * @author admin
 *
 */
public class BussOperatorController extends BaseController {
	private boolean success = false;
	
	private final static String BASE_PAGE_URL = "buss_operator";

	/***
	 * 进入列表页
	 */
	public void index() {
		render(BASE_PAGE_URL+"_index.jsp");
	}

	/***
	 * 根据id是否为空，为空进入新增页，不为空进入修改页
	 */
	public void au_page() {
		Integer selectId = getParaToInt("selectId", 0);
		if (selectId != null && selectId > 0) {
			BussOperator item = BussOperator.dao.findById(selectId);
			setAttr("bussOperator", item);
		}
		render(BASE_PAGE_URL+"_au.jsp");
	}

	/***
	 * 获取列表页数据
	 */
	public void list() {
		String name = getPara("name");
		String accountName = getPara("accountName");
		String phone = getPara("phone");
		String cardNo = getPara("cardNo");
		int page = getParaToInt("page", 1);
		int pageSize = getParaToInt("rows", 10);
		Page<BussOperator> pager = BussOperator.dao.findPage(page, pageSize, name,accountName, phone, cardNo);
		setAttr("total", pager.getTotalRow());
		setAttr("rows", pager.getList());
		renderResult(ResultStatus.SUCCESS);
	}

	/***
	 * 根据id是否为空判断为新增还是修改，为空则为新增，不为空则为修改
	 */
	@Before(Tx.class) 
	public void save() {
		try {
			
			String url = super.upload("head_img");
			System.out.println("url=:"+url);
			Users u = getCurUser();
			BussOperator item = getModel(BussOperator.class);
			if (item.getInt("id") == null || item.getInt("id") == 0) {
				item.set("head_img", url);
				setCreator(item, u);
				success = item.save();
				if (success){
					String uuid = super.getUUID();
					Users user = new Users();
					user.set("login_name", item.get("account_name"));
					user.set("user_name", item.get("name"));
					user.set("sex", item.getInt("sex") != null && item.getInt("sex") == 0 ? "男" : "女");
					user.set("password", MD5.md5("123456").substring(4, 28));
					user.set("email", item.get("email"));
					user.set("phone", item.get("phone"));
					user.set("uuid", uuid);
					user.save();
					
					List<Groups> result = Groups.dao.checkGroup(GROUP_TYPE_3);
					if(result == null || result.size() == 0){
						Groups group = new Groups();
						group.set("group_name", "摊位经营户角色");
						group.set("group_type",GROUP_TYPE_3);
						group.save();
						user.set("group_id", group.get("id"));
					} else {
						Groups groupItem = result.get(0);
						user.set("group_id", groupItem.get("id"));
					}
					
					int userId = user.getInt("user_id");
					item.set("user_id", userId);
					item.set("user_uid", uuid);
					item.update();
					
					user.update();
					
					renderResult(ResultStatus.SUCCESS);
				}
				else
					renderResult(ResultStatus.ADD_FAILURL);
			} else {
				if(!url.equals(item.get("oldHeadImg"))){
					item.set("head_img", url);
				}
				setOperator(item, u);
				success = item.update();
				if (success)
					renderResult(ResultStatus.SUCCESS);
				else
					renderResult(ResultStatus.UPDATE_FAILURE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			renderResult(ResultStatus.EXCEPTION);
		}
	}

	/***
	 * 删除
	 */
	public void delete() {
		Integer selectId = getParaToInt("selectId", 0);
		if (selectId > 0) {
			
			success = BussOperator.dao.deleteById(selectId, getCurUser());
			if (success){
				BussOperator item = BussOperator.dao.findById(selectId);
				Integer userId = item.getInt("user_id");
				Users.dao.deleteById(userId);
				renderResult(ResultStatus.SUCCESS);
			}
				
			else
				renderResult(ResultStatus.DELETE_FAILURE);
		}
	}


}
